<template>
  <div>
    <el-form :model="SignFrom" :rules="loginRules" ref="SignFrom" class="login-form" autocomplete="on" label-position="left">
      <el-form-item prop="username">
        <el-input class="el-input" v-model="SignFrom.username" auto-complete="off" placeholder="姓名">
          <i slot="prefix" class="el-input__icon iconfont sigin-iconfont">&#xe66f;</i>
        </el-input>
      </el-form-item>
      <el-form-item prop="email">
        <el-input v-model="SignFrom.email" auto-complete="off" placeholder="Email地址">
          <i slot="prefix" class="el-input__icon iconfont sigin-iconfont">&#xe611;</i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="SignFrom.password" placeholder="密码">
          <i slot="prefix" class="el-input__icon iconfont sigin-iconfont">&#xe734;</i>
        </el-input>
      </el-form-item>
      <el-form-item class="login-button">
        <el-button @click="submitForm" type="danger">免费注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
export default {
  name: 'UserSignin',
  props:{
      initFormData:Object
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('The user name can not be null'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if(value.trim && value.length >= 6){
        callback()
      }else{
        callback(new Error("The password can not be less than 6 digits"))
      }
    }
    const validateEmail = (rule, value, callback) => {
      const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      var valid = reg.test(value)
      if (!valid) {
        callback(new Error('Please enter the correct email'))
      } else {
        callback()
      }
    }
    return {
      SignFrom: this.initFormData,
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        email: [{ required: true, trigger: 'blur', validator: validateEmail }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  mounted(){

  },
  methods:{
    submitForm(){
       this.$refs.SignFrom.validate(valid =>{
         console.log(this.initFormData)
         if(valid){
       this.$alert('注册成功，请重新登陆！', {
          confirmButtonText: '确定',
          callback: action => {
           this.$emit('changActive','first')
          }
        })
         }
       })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
